﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WSTest.AITSService;
using WSTest.EfbService;

namespace WSTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {        
        private EfbService.service ser = new service();
        private AuthenticationService  aitsService = new AuthenticationService();
        private EfbService.Message mess;
        private List<Devices> listDevices = new List<Devices>();
        private com.vietnamairlines.efb.service _efbService = new com.vietnamairlines.efb.service();
        private TestWS.service _testWS = new TestWS.service();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }

        //<Email>Cv2@vietnamairlines.com</Email><Password>Vna@2014</Password><Uuid>D075A193-50A5-4D1E-A918-A2BAAECBF258</Uuid>
        //<AppID>com.aits.vietnamairlines.VNAEDOC</AppID><Push_ID>User disable push service</Push_ID></DevicesActive>


        protected void btCheckDevice_Click(object sender, EventArgs e)
        {
            mess = ser.CheckDeviceActive("0C6DB99A-51AB-4626-AC69-323F7815DAB6");

            lblNote.Text = "Check Device : " + mess.Title + " - " + mess.Result;
        }

        protected void btActive_Click(object sender, EventArgs e)
        {
            
            string uuid = "D075A193-50A5-4D1E-A918-A2BAAECBF258";
            //string email = "Cv2@vietnamairlines.com";
            //string pass = "Vna@2013";
            string appId = "com.aits.vietnamairlines.VNAEDOC";
            string pushId = "";
            //var mess = ser.DevicesActive(uuid, email, pass, appId, pushId);

            //lblNote.Text = mess.Result + " - " + mess.Title;

            var mess = ser.DevicesActive(uuid, txtEmail.Text.Trim(), txtPass.Text.Trim(), appId, pushId);
            lblNote.Text = mess.Result + " - " + mess.Content;
            //var mess = aitsService.Authenticate(txtEmail.Text.Trim(), txtPass.Text.Trim());
            //lblNote.Text = mess.ToString();
        }

        protected void btLogDownload_Click(object sender, EventArgs e)
        {
            mess = ser.LogDownload("TEST", "com.aits.vietnamairlines.VNAEDOC", "test@gmail.com", "167A3AD5-AAF7-4EBA-AA38-526186B5AD52", "Version:2.0.0 (built on:2015-03-17 16:22)", "N/A", "iPad Simulator","");

            lblNote.Text = "LogDownload : " + mess.Title + " - " + mess.Result;
        }


        public class Message
        {
            public string Title { get; set; }
            public string Content { get; set; }

            public Boolean Result { get; set; }
        }

        protected void btCheckUserAITS_Click(object sender, EventArgs e)
        {
            autheTest.AuthenticationServiceSoapClient client = new autheTest.AuthenticationServiceSoapClient();
            client.Authenticate("");

            //lblNote.Text = "CheckUserAITS : " + aitsService.Authenticate("helpdesk.pmd", "Hd@smd13", 0);
            //lblNote.Text = "CheckUserAITS : " + aitsService.Authenticate("CV2@vietnamairlines.com", "Vna@2013", 0);
            lblNote.Text = "Check WS3 : " + ser.DevicesActive("D075A193-50A5-4D1E-A918-A2BAAECBF258","helpdesk.pmd", "Hd@smd13","com.aits.vietnamairlines.VNAEDOC","sadfsdfda").Content;
        }

        private void Rollback2()
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["vna"].ToString()))
            {
                connection.Open();

                SqlCommand cmd = connection.CreateCommand();
                SqlTransaction transaction;

                // Start a local transaction.
                transaction = connection.BeginTransaction("SampleTransaction");

                // Must assign both transaction object and connection 
                // to Command object for a pending local transaction
                cmd.Connection = connection;
                cmd.Transaction = transaction;

                try
                {
                    //command.CommandText =
                    //    "Insert into FTP (HostFTP) VALUES ('HostFTP')";
                    //command.ExecuteNonQuery();
                    //command.CommandText =
                    //    "Insert into Region (HostFTP) VALUES ('HostFTP 2')";
                    //command.ExecuteNonQuery();

                    cmd.CommandText = "VNA_Insert_LogDownload";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Username", "tiennv"));
                    cmd.Parameters.Add(new SqlParameter("@AppID", "com.vna.com.vn"));
                    cmd.Parameters.Add(new SqlParameter("@Uuid", "sfdsdfsdfsdfs"));
                    cmd.Parameters.Add(new SqlParameter("@Email", "tiennv@yahoo.com"));

                    string id = cmd.ExecuteScalar().ToString();


                    cmd.CommandText = "VNA_Insert_LogDownload";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Username", "tiennv"));
                    cmd.Parameters.Add(new SqlParameter("@AppID", "com.vna.com.vn"));
                    cmd.Parameters.Add(new SqlParameter("@Uuid", "sfdsdfsdfsdfs"));
                    cmd.Parameters.Add(new SqlParameter("@Email", "tiennv@yahoo.com"));

                    string id2 = cmd.ExecuteScalar().ToString();


                    lblNote.Text = id + " - " + id2;
                    // Attempt to commit the transaction.
                    transaction.Commit();
                    Console.WriteLine("Both records are written to database.");
                }
                catch (Exception ex)
                {
                    string str1 = "Commit Exception Type: " + ex.GetType();
                    string str2 = "  Message: " + ex.Message;
                    lblNote.Text = str1 + " - " + str2;
                    // Attempt to roll back the transaction. 
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        // This catch block will handle any errors that may have occurred 
                        // on the server that would cause the rollback to fail, such as 
                        // a closed connection.
                        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                        Console.WriteLine("  Message: {0}", ex2.Message);
                    }
                }
            }
        }

        private void Rollback1()
        {
            using (var Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["vna"].ToString()))
            {
                SqlTransaction trans = null;
                try
                {
                    Conn.Open();
                    trans = Conn.BeginTransaction();
                    string id = "";
                    using (SqlCommand cmd = new SqlCommand("VNA_Insert_LogDownload", Conn, trans))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Username", "tiennv"));
                        cmd.Parameters.Add(new SqlParameter("@AppID", "com.vna.com.vn"));
                        cmd.Parameters.Add(new SqlParameter("@Uuid", "sfdsdfsdfsdfs"));
                        cmd.Parameters.Add(new SqlParameter("@Email", "tiennv@yahoo.com"));

                        id = cmd.ExecuteScalar().ToString();
                    }

                    using (SqlCommand cmd = new SqlCommand("VNA_Insert_LogDownload", Conn, trans))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Username", "tiennv"));
                        cmd.Parameters.Add(new SqlParameter("@AppID", "com.vna.com.vn"));
                        cmd.Parameters.Add(new SqlParameter("@Uuid", "sfdsdfsdfsdfs"));
                        cmd.Parameters.Add(new SqlParameter("@Email", "tiennv@yahoo.com"));

                        id = cmd.ExecuteScalar().ToString();
                    }

                    trans.Commit();
                }
                catch (Exception Ex)
                {
                    if (trans != null) trans.Rollback();
                    //return -1;
                }
            }
        }
        protected void btRollback_Click(object sender, EventArgs e)
        {
            Rollback1();
        }

        protected void btNewVersion_Click(object sender, EventArgs e)
        {
            var result = ser.NewVersion("com.aits.vietnamairlines.VNAEDOC", "2.0.1", "2015-01-07 23:46:00.000");

            lblNote.Text = "New Version : " + result.Version + " - " + result.InstallLink;
        }
    }
}